package org.jeecg.modules.geo.service.resolve.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.bim.entity.BimPointCloud;
import org.jeecg.modules.bim.service.IBimPointCloudService;
import org.jeecg.modules.geo.core.constant.BimGeoEventType;
import org.jeecg.modules.geo.entity.BimGeoEvent;
import org.jeecg.modules.geo.model.dto.PointCloudDTO;
import org.jeecg.modules.geo.model.dto.point.Point;
import org.jeecg.modules.geo.model.vo.point.PointVo;
import org.jeecg.modules.geo.service.resolve.GeoBaseResolve;
import org.jeecg.modules.geo.service.resolve.GeoBaseService;
import org.jeecg.modules.geo.service.resolve.IGeoResolves;
import org.jeecg.modules.icon.entity.BimGeoIcon;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @ClassName
 * @Description TODO
 * @Author czd
 * @Date 2021/12/27 9:56
 * @Version 1.0
 **/
@Service
@Slf4j
public class CloudsGeoResolveServiceImpl implements IGeoResolves {

    @Autowired
    private IBimPointCloudService bimPointCloudService;

    public List<PointCloudDTO> resolveCloud(List<PointCloudDTO> pointClouds, List<BimGeoEvent> geoEventConfigResults, List<BimGeoIcon> geoIconConfigResults) {
        if (pointClouds == null || (geoEventConfigResults == null && geoIconConfigResults == null)) {
            return pointClouds;
        }

        Map<Integer, Map<Integer, List<BimGeoEvent>>> defaultEventsAll = GeoBaseService.getGeoDefaultEvent(geoEventConfigResults, BimGeoEventType.POINT_EVENT_TYPE.getValue());
        Map<String, List<BimGeoEvent>> eventsGroupByName = GeoBaseService.getGeoEventGroupbyName(geoEventConfigResults, BimGeoEventType.POINT_EVENT_TYPE.getValue());

        Map<Integer, List<BimGeoIcon>> pointClouddefaultIcons = GeoBaseService.getGeoDefaultIcons(geoIconConfigResults, BimGeoEventType.POINT_EVENT_TYPE.getValue());
        Map<String, List<BimGeoIcon>> pointCloudGroupByName = GeoBaseService.getGeoIconGroupByName(geoIconConfigResults, BimGeoEventType.POINT_EVENT_TYPE.getValue());

        Map<Integer, List<BimGeoIcon>> pointDefaultIcons = GeoBaseService.getGeoDefaultIcons(geoIconConfigResults, BimGeoEventType.POINT_CSS_TYPE.getValue());
        Map<String, List<BimGeoIcon>> pointGroupByName = GeoBaseService.getGeoIconGroupByName(geoIconConfigResults, BimGeoEventType.POINT_CSS_TYPE.getValue());
        //解析鼠标事件
        for (PointCloudDTO pointCloud : pointClouds) {
            //List<String> leftAction = pointCloud.getLeftAction();
            String customPointCloudCss = pointCloud.getCustomPointCloudCss();
            if (pointClouddefaultIcons != null || (StrUtil.isNotEmpty(customPointCloudCss) && pointCloudGroupByName != null)) {
                HashMap<String, Object> icons = GeoBaseResolve.resolveIcons(pointClouddefaultIcons, pointCloudGroupByName, customPointCloudCss, BimGeoEventType.POINT_EVENT_TYPE.getValue());
                pointCloud.setPointCloudCss(icons);
            }

            List<Point> points = pointCloud.getPoints();
            for (Point point : points) {
                HashMap<String, Object> clicks = new HashMap<>();
                String leftAction = pointCloud.getLeftAction();
                if ((leftAction != null && !leftAction.isEmpty() && eventsGroupByName != null) || defaultEventsAll != null) {
                    //默认只有一个事件
                    Map<String, Object> clickLeft = GeoBaseResolve.resolveLeftEvent(defaultEventsAll, eventsGroupByName, leftAction, point.getParams(), BimGeoEventType.POINT_EVENT_TYPE.getValue());
                    clicks.put("clickLeft", clickLeft);
                }
                String rightAction = pointCloud.getRightAction();
                if ((rightAction != null && !rightAction.isEmpty() && eventsGroupByName != null) || defaultEventsAll != null) {
                    Map<String, Object> clickRight = GeoBaseResolve.resolveRightEvent(defaultEventsAll, eventsGroupByName, rightAction, point.getParams(), BimGeoEventType.POINT_EVENT_TYPE.getValue());
                    clicks.put("clickRight", clickRight);
                }
                String customPointCss = point.getCustomPointCss();
                if (pointDefaultIcons != null || (StrUtil.isNotEmpty(customPointCss) && pointGroupByName != null)) {
                    Map<String, Object> icons = GeoBaseResolve.resolveIcons(pointDefaultIcons, pointGroupByName, customPointCss, BimGeoEventType.POINT_CSS_TYPE.getValue());
                    point.setPointCss((HashMap<String, Object>) icons);
                }

                point.setAction(clicks);
            }
        }
        return pointClouds;
    }

    @Override
    public List<PointCloudDTO> resolveCloud(List<PointCloudDTO> pointClouds, Long projectId) {
        //查询系统项目下所有的点云信息
        List<BimPointCloud> bimPointClouds = bimPointCloudService.pointCloudList(null, projectId);
        if (CollUtil.isEmpty(bimPointClouds)) {
            log.info("查询系统下项目配置点云数据为空,第三方项目id=>{}", projectId);
            return pointClouds;
        }
        //map的key对应业务系统点云的customPointCss
        Map<String, BimPointCloud> cloudMap = bimPointClouds.stream().collect(Collectors.toMap(BimPointCloud::getName, item -> item));
        //遍历处理数据
        for (PointCloudDTO pointCloud : pointClouds) {
            //业务系统点位信息
            List<Point> points = pointCloud.getPoints();
            for (Point point : points) {
                //获取点云名称
                String name = point.getCustomPointCss();
                if (StrUtil.isNotBlank(name)) {
                    BimPointCloud bimPointCloud = cloudMap.get(name);
                    if (bimPointCloud != null && StrUtil.isNotBlank(bimPointCloud.getPointCloud())) {
                        PointVo pointVo = JSON.parseObject(bimPointCloud.getPointCloud(), PointVo.class);
                        point.setAction(pointVo.getAction());
                        //点云样式
                        point.setPointCss(pointVo.getPointCss());
                    }
                }
            }
        }
        return pointClouds;
    }
}
